<?php

class Edit extends TPage
{
	public $element_manager;
	public $status_manager;
	private $place_manager;
	private $address_manager;
	public $element;
	public $place = null;
	public $address = null;
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->element_manager = $this->Application->Modules['conference_elements'];
		$this->status_manager = $this->Application->Modules['statuses'];
		$this->place_manager = $this->Application->Modules['places'];
		$this->address_manager = $this->Application->Modules['addresses'];
		
		$this->element = $this->element_manager->getConferenceElementById($this->Request['id']);
		if(!($this->element instanceof ConferenceElementRecord))
			$this->Response->redirect($this->Service->constructUrl('organizer.conference_elements.Add'));
	
		$this->place = $this->place_manager->getPlaceById($this->element->getPlaceId());
		if($this->place)
			$this->address = $this->address_manager->getAddressById($this->place->getAddressId());
			
		if(!$this->IsPostback)
		{
			$this->readData();
		}
	}
	
	public function readData()
	{
		$this->name->Text = $this->element->getName();
		$this->is_session->Checked = $this->element->getIsSession();
		$this->start_datetime->setDateTime($this->element->getStartDatetime());
		$this->duration->Text = $this->element->getDuration();
		$this->price_checked->Checked = $this->element->getPrice() !== null;
		$this->price->Text = $this->element->getPrice() !== null ? $this->element->getPrice() : '';
		$this->quantity_checked->Checked = $this->element->getQuantity() !== null;
		$this->quantity->Text = $this->element->getQuantity() !== null ? $this->element->getQuantity() : '';
		$this->description_checked->Checked = $this->element->getDescription() !== null;
		$this->description->Text = $this->element->getDescription() !== null ? $this->element->getDescription() : '';
		//owner_id
		$this->is_plan->Checked = $this->element->getIsPlan();
		
		if($this->place)
		{
			$this->place_checked->Checked = true;
			
			$this->place_name->Text = $this->place->getName();
			$this->place_description->Text = $this->place->getDescription();
			if($this->address)
			{
				$this->address_checked->Checked = true;
				
				$this->address_form->Country->Text = $this->address->getCountry();
				$this->address_form->City->Text = $this->address->getCity();
				$this->address_form->PostalCode->Text = $this->address->getPostalCode();
				$this->address_form->Street->Text = $this->address->getStreet();
				$this->address_form->Number->Text = $this->address->getNumber();
				$this->address_form->Description->Text = $this->address->getDescription();
			}
		}
	}
	
	public function saveButtonClicked($sender, $params)
	{
		if($this->Page->IsValid)
		{
			$this->element->setName(String::format($this->name->Text));
			//$this->element->setIsSession($this->is_session->getChecked());
			$this->element->setStartDatetime($this->start_datetime->getDateTime());
			$this->element->setDuration($this->duration->Text);
			$this->element->setPrice($this->price_checked->Checked ? $this->price->Text : null);
			$this->element->setQuantity($this->quantity_checked->Checked ? $this->quantity->Text : null);
			$this->element->setDescription($this->description_checked->Checked ? String::format($this->description->Text) : null);
			//$this->element->setOwnerId();
			$this->element->setIsPlan($this->is_plan->getChecked());
			
			$place_id = $this->savePlace();
			$this->element->setPlaceId($place_id);
			
			$result = $this->element_manager->updateConferenceElementByConferenceElementRecord(
													$this->element
												);

			if($result)
				$this->info->Text = '<p>'.Prado::localize('Changes saved').'</p>';
			else
				$this->info->Text = '<p>'.Prado::localize('Changes not saved').'</p>';
		}
	}
	
	public function savePlace()
	{
		$place_id = null;
		$address_id = null;
		
		if($this->place_checked->Checked)
		{
			if($this->address_checked->Checked)
			{
				if($this->address)
				{
					$result = $this->address_manager->updateAddress(
														$this->address->getId(),
														String::format($this->address_form->Country->Text),
														String::format($this->address_form->City->Text),
														String::format($this->address_form->PostalCode->Text),
														String::format($this->address_form->Street->Text),
														String::format($this->address_form->Number->Text),
														String::format($this->address_form->description->Text));
				}
				else
				{
					$result = $this->address_manager->addAddress(
														String::format($this->address_form->Country->Text),
														String::format($this->address_form->City->Text),
														String::format($this->address_form->PostalCode->Text),
														String::format($this->address_form->Street->Text),
														String::format($this->address_form->Number->Text),
														String::format($this->address_form->description->Text));
					
				}
				if($result)
					$address_id = $result->getId();
			}
			else
			{
				if($this->address)
				{
					$this->address_manager->deleteAddress($this->address->getId());
					$this->address_form->clearForm();
				}
			}
				
			if($this->place)
			{
				$result = $this->place_manager->updatePlace(
													$this->place->getId(),
													String::format($this->place_name->Text),
													String::format($this->place_description->Text),
													$address_id);
			}
			else
			{
				$result = $this->place_manager->addPlace(
													String::format($this->place_name->Text),
													String::format($this->place_description->Text),
													$address_id);
				
			}
			if($result)
				$place_id = $result->getId();
			
		}
		else
		{
			if($this->place)
				$this->place_manager->deletePlace($this->place->getId());
			$this->place_name->Text = '';
			$this->place_description->Text = '';
			$this->address_form->clearForm();
		}
		
		return $place_id;
	}
	
	public function savePlace2()
	{
		$place_id = null;
		if($this->place_checked->Checked)
		{
			if($this->address_checked->Checked)
			{
				$address_id = null;
				if($this->address)
				{
					$address_id = $this->address->getId();
					$result = $this->address_manager->updateAddress(
														$this->address->getId(),
														String::format($this->address_form->Country->Text),
														String::format($this->address_form->City->Text),
														String::format($this->address_form->PostalCode->Text),
														String::format($this->address_form->Street->Text),
														String::format($this->address_form->Number->Text),
														String::format($this->address_form->description->Text));
				}
				else
				{
					$result = $this->address_manager->addAddress(
														String::format($this->address_form->Country->Text),
														String::format($this->address_form->City->Text),
														String::format($this->address_form->PostalCode->Text),
														String::format($this->address_form->Street->Text),
														String::format($this->address_form->Number->Text),
														String::format($this->address_form->description->Text));
					if($result)
						$address_id = $result->getId();
				}
			}
			else
			{
				if($this->address)
					$this->address_manager->deleteAddress($this->address->getId());
				$address_id = null;
			}
				
			if($this->place)
			{
				$place_id = $this->place->getId();
				$result = $this->place_manager->updatePlace(
													$this->place->getId(),
													String::format($this->place_name->Text),
													String::format($this->place_description->Text),
													$address_id);
			}
			else
			{
				$result = $this->place_manager->addPlace(
													String::format($this->place_name->Text),
													String::format($this->place_description->Text),
													$address_id);
				if($result)
					$place_id = $result->getId();
			}
			
		}
		else
		{
			if($this->place)
				$this->place_manager->deletePlace($this->place->getId());
			$place_id = null;
		}
		
		return $place_id;
	}
	
	public function addPlaceVisibleButtonClicked()
	{
		$this->place_panel->Visible = $this->place_checked->Checked;
		$this->place_panel->Enabled = $this->place_checked->Checked;
	}
	
	public function addAddressVisibleButtonClicked()
	{
		$this->address_panel->Visible = $this->address_checked->Checked;
		$this->address_panel->Enabled = $this->address_checked->Checked;
	}
	
	public function getCurrencyName()
	{
		$setting_manager = $this->Application->Modules['settings'];
		if($setting = $setting_manager->getSettingByName(SettingManager::$CURRENCY_NAME))
			return $setting->getValue() != '' ? $setting->getValue() : '-';
		return '-';
	}
}

?>